import axios from "axios"
import router from "../config/router";
import { message } from 'ant-design-vue'
// 添加请求拦截器
axios.interceptors.request.use(function(config) {
    // 在发送请求之前做些什么
    localStorage.getItem("userInfo")?config.headers.common['Authorization'] = 'Bearer ' + JSON.parse(localStorage.getItem("userInfo")).token:'';
    //config.headers.post["Content-type"] = 'application/x-www-form-urlencoded;charset=UTF-8'
    return config;
}, function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});


// 添加响应拦截器
axios.interceptors.response.use(function(response) {
    // 对响应数据做点什么
    if (response.data.code == 401) {
        message.error(response.data.message, 5)
        sessionStorage.clear()
        router.replace({
            path: '/login',
            query: {
                redirect: router.currentRoute.fullPath
            }
        })
        return;
    }
    return response;
}, function(error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

let baseUrl = '/api';
// let baseUrl = 'http://180.76.236.187:8088';


//get请求
export const GET = (url, params) => {
    return axios.get(`${baseUrl}${url}`, params, {}).then(data => data)
}


//post请求
export const POST = (url, data) => {
    return axios.post(`${baseUrl}${url}`, data, {}).then(data => data)
}

//delete请求
export const DELETE = (url, params) => {
    return axios.delete(`${baseUrl}${url}`, params, {}).then(data => data)
}


//put请求
export const PUT = (url, data) => {
    return axios.put(`${baseUrl}${url}`, data, {}).then(data => data)
}